package com.lznb.cloud.controller;

import com.lznb.cloud.entity.NewsCategory;
import com.lznb.cloud.service.NewsCategoryService;
import com.lznb.cloud.utils.JsonResultExt;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.StringWriter;
import java.util.*;

/**
 * 栏目
 *
 * @author mzy
 * @email 1992lcg@163.com
 * @date ${datetime}
 */

@Controller
@RequestMapping("/newsCategoryController")
public class NewsCategoryController {
    @Autowired
    Configuration configuration;
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private NewsCategoryService newsCategoryService;

    /**
     * 根据主键查询
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/get")
    @ResponseBody
    NewsCategory get(String categoryId) {
        logger.info("根据主键查询");
        return newsCategoryService.getNewsCategory(categoryId);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ResponseBody
    JsonResultExt save(NewsCategory newsCategory) {
        String id=UUID.randomUUID().toString().replace("-","");
        newsCategory.setCategoryId(id);
       if(StringUtils.isEmpty(newsCategory.getCategoryPid())){
           newsCategory.setCategoryPid(id);
       }
        logger.info("栏目保存");
        newsCategory.setCreateTime(new Date());
        newsCategory.setUpdateTime(new Date());
        return newsCategoryService.save(newsCategory);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ResponseBody
    JsonResultExt update(NewsCategory newsCategory) {
        logger.info("栏目修改");
        newsCategory.setUpdateTime(new Date());
        return newsCategoryService.update(newsCategory);
    }

    /**
     * 删除
     */
    @GetMapping("/delete")
    @ResponseBody
    JsonResultExt delete(String categoryId) {
        logger.info("栏目删除");

        return newsCategoryService.delete(categoryId);
    }


    /**
     * 分页查询
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/findByPage")
    @ResponseBody
    Map findByPage(@RequestParam(value = "page", defaultValue = "0") Integer page,
                   @RequestParam(value = "limit", defaultValue = "15") Integer limit, NewsCategory newsCategory) {
        logger.info("栏目分页查询");
        Map<String, String> condition = new HashMap<String, String>();
        condition.put("categoryName", "like");
        return newsCategoryService.findByPage(page - 1, limit, newsCategory, condition);
    }

    @GetMapping("/toList")
    @ResponseBody
    public String toList(Model model) {

        return toPage(model, "/pages/newsCategory/list.ftl");
    }

    @GetMapping("/toAdd")
    @ResponseBody
    public String toAdd(Model model) {
        List<NewsCategory> categories = newsCategoryService.findAll();
        model.addAttribute("categories",categories);
        return toPage(model, "/pages/newsCategory/add.ftl");
    }

    @GetMapping("/toUpdate")
    @ResponseBody
    public String toUpdate(String categoryId, Model model) {
        List<NewsCategory> categories = newsCategoryService.findAll();
        model.addAttribute("categories",categories);
        NewsCategory newsCategory = newsCategoryService.getNewsCategory(categoryId);
        model.addAttribute("newsCategory", newsCategory);
        return toPage(model, "/pages/newsCategory/edit.ftl");
    }


    /**
     * 页面跳转
     *
     * @param model
     * @param pageUrl
     * @return
     */
    public String toPage(Model model, String pageUrl) {
        Template template = null;
        try {
            template = configuration.getTemplate(pageUrl);
            StringWriter stringWriter = new StringWriter();
            template.process(model, stringWriter);
            return stringWriter.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
        return "";
    }
}
